package com.example.mydy.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.mydy.R;
import com.example.mydy.model.Project;
import com.example.mydy.utils.MockDataManager;
import com.example.mydy.utils.SharedPreferencesManager;

public class ProjectDialogFragment extends DialogFragment {

    private static final String ARG_PROJECT_ID = "project_id";
    private static final String ARG_PROJECT_NAME = "project_name";
    private static final String ARG_PROJECT_PRICE = "project_price";

    private int projectId;
    private String projectName;
    private double projectPrice;
    private ProjectUnlockListener unlockListener;

    public static ProjectDialogFragment newInstance(Project project) {
        ProjectDialogFragment fragment = new ProjectDialogFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PROJECT_ID, project.getId());
        args.putString(ARG_PROJECT_NAME, project.getName());
        args.putDouble(ARG_PROJECT_PRICE, project.getPrice());
        fragment.setArguments(args);
        return fragment;
    }

    public void setUnlockListener(ProjectUnlockListener listener) {
        this.unlockListener = listener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            projectId = getArguments().getInt(ARG_PROJECT_ID);
            projectName = getArguments().getString(ARG_PROJECT_NAME);
            projectPrice = getArguments().getDouble(ARG_PROJECT_PRICE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_unlock_project, container, false);
        
        TextView titleTextView = view.findViewById(R.id.dialog_title);
        TextView messageTextView = view.findViewById(R.id.dialog_message);
        Button cancelButton = view.findViewById(R.id.btn_cancel);
        Button confirmButton = view.findViewById(R.id.btn_confirm);

        // 设置对话框内容
        titleTextView.setText(R.string.project_unlock_title);
        String message = getString(R.string.project_unlock_message, String.valueOf(projectPrice));
        messageTextView.setText(message);

        // 设置按钮点击事件
        cancelButton.setOnClickListener(v -> dismiss());
        confirmButton.setOnClickListener(v -> unlockProject());

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setLayout(width, height);
        }
    }

    private void unlockProject() {
        String username = SharedPreferencesManager.getUsername(requireContext());
        
        boolean success = MockDataManager.getInstance().unlockProject(username, projectId, projectPrice);
        
        if (success) {
            Toast.makeText(requireContext(), R.string.project_unlock_success, Toast.LENGTH_SHORT).show();
            if (unlockListener != null) {
                unlockListener.onProjectUnlocked(projectId);
            }
            dismiss();
        } else {
            // 检查失败原因
            if (MockDataManager.getInstance().hasUnlockedProject(username, projectId)) {
                Toast.makeText(requireContext(), R.string.project_already_unlocked, Toast.LENGTH_SHORT).show();
                dismiss();
            } else {
                Toast.makeText(requireContext(), R.string.balance_not_enough, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public interface ProjectUnlockListener {
        void onProjectUnlocked(int projectId);
    }
} 